<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // forEach 没有返回值 不会改变原数组 循环遍历
        var arr = ["张三","李四","王五"];
        // --------------原生方法-------------------------------------------
        arr.forEach(function(item,key,arr){
            console.log(item,key,arr);
        })

        // --------------用for底层来写-------------------------------------------
        function myForEach(arr,cb){
            for(var i=0;i<arr.length;i++){
                cb(arr[i],i,arr);
            }
        }
        myForEach(arr,function(item,key,arr){
            console.log(item,key,arr);
        })
        // --------------挂在到原型上-------------------------------------------
        Array.prototype.myForEach = function(cb){
            for(var i=0;i<this.length;i++){
                cb(this[i],i,this);
            }
        }

        arr.myForEach(function(item,key,arr){
            console.log(item,key,arr);
        })
    </script>
</body>
</html>